package com.wst.system.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wst.system.config.exception.GlobalExcetion;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wst.system.server.entity.SysDict;
import com.wst.system.server.mapper.SysDictMapper;
import com.wst.system.server.service.SysDictService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.BeanUtils;
import com.wst.system.server.entity.vo.SysDictVo;
import java.io.Serializable;
import com.wst.system.server.entity.vo.PageSearchVo;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author X.I.O
 * @since 2025-02-28
 */
@Service
@Transactional
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {


    @Override
    public SysDict getById(Serializable id){
        return super.getById(id);
    }


    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }


    @Override
    public boolean updateById(SysDict sysdict) {
        return super.updateById(sysdict);
    }


    public SysDict saveSysDict(SysDict sysdict) throws GlobalExcetion {
        super.save(sysdict);
        return sysdict;
    }


    public SysDict updateSysDictById(SysDict sysdict) throws GlobalExcetion{
        super.updateById(sysdict);
        return sysdict;
    }


    public SysDict saveOrUpdateSysDict(SysDictVo sysdictVo) throws GlobalExcetion{
        SysDict sysdict=new SysDict();
        BeanUtils.copyProperties(sysdictVo,sysdict);
        if(sysdictVo.getId()!=null){
            SysDict sysdictEntity=super.baseMapper.selectById(sysdictVo.getId());
            if(sysdictEntity==null){
                throw new GlobalExcetion(1,"数据不存在");
            }
            super.baseMapper.updateById(sysdict);
        }else{
            super.baseMapper.insert(sysdict);
        }
        return sysdict;
    }

    public IPage<SysDict> page(PageSearchVo vo) throws GlobalExcetion{
        LambdaQueryWrapper<SysDict> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        IPage<SysDict> iPage=super.baseMapper.selectPage(new Page(vo.getPageNum(), vo.getPageSize()),lambdaQueryWrapper);
        return iPage;
    }
}